/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.iotdb.db.mpp.aggregation;

import org.apache.iotdb.db.mpp.execution.operator.window.IWindow;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
import org.apache.iotdb.tsfile.read.common.block.column.Column;

public class FirstValueDescAccumulator extends FirstValueAccumulator {

    public FirstValueDescAccumulator(TSDataType seriesDataType) {
        super(seriesDataType);
    }

    @Override
    public boolean hasFinalResult() {
        return false;
    }

    // Don't break in advance
    protected int addIntInput(Column[] column, IWindow curWindow) {
        int curPositionCount = column[0].getPositionCount();

        for (int i = 0; i < curPositionCount; i++) {
            // skip null value in control column
            if (column[0].isNull(i)) {
                continue;
            }
            if (!curWindow.satisfy(column[0], i)) {
                return i;
            }
            curWindow.mergeOnePoint();
            if (!column[2].isNull(i)) {
                updateIntFirstValue(column[2].getInt(i), column[1].getLong(i));
            }
        }
        return curPositionCount;
    }

    protected int addLongInput(Column[] column, IWindow curWindow) {
        int curPositionCount = column[0].getPositionCount();

        for (int i = 0; i < curPositionCount; i++) {
            // skip null value in control column
            if (column[0].isNull(i)) {
                continue;
            }
            if (!curWindow.satisfy(column[0], i)) {
                return i;
            }
            curWindow.mergeOnePoint();
            if (!column[2].isNull(i)) {
                updateLongFirstValue(column[2].getLong(i), column[1].getLong(i));
            }
        }
        return curPositionCount;
    }

    protected int addFloatInput(Column[] column, IWindow curWindow) {
        int curPositionCount = column[0].getPositionCount();

        for (int i = 0; i < curPositionCount; i++) {
            // skip null value in control column
            if (column[0].isNull(i)) {
                continue;
            }
            if (!curWindow.satisfy(column[0], i)) {
                return i;
            }
            curWindow.mergeOnePoint();
            if (!column[2].isNull(i)) {
                updateFloatFirstValue(column[2].getFloat(i), column[1].getLong(i));
            }
        }
        return curPositionCount;
    }

    protected int addDoubleInput(Column[] column, IWindow curWindow) {
        int curPositionCount = column[0].getPositionCount();

        for (int i = 0; i < curPositionCount; i++) {
            // skip null value in control column
            if (column[0].isNull(i)) {
                continue;
            }
            if (!curWindow.satisfy(column[0], i)) {
                return i;
            }
            curWindow.mergeOnePoint();
            if (!column[2].isNull(i)) {
                updateDoubleFirstValue(column[2].getDouble(i), column[1].getLong(i));
            }
        }
        return curPositionCount;
    }

    protected int addBooleanInput(Column[] column, IWindow curWindow) {
        int curPositionCount = column[0].getPositionCount();

        for (int i = 0; i < curPositionCount; i++) {
            // skip null value in control column
            if (column[0].isNull(i)) {
                continue;
            }
            if (!curWindow.satisfy(column[0], i)) {
                return i;
            }
            curWindow.mergeOnePoint();
            if (!column[2].isNull(i)) {
                updateBooleanFirstValue(column[2].getBoolean(i), column[1].getLong(i));
            }
        }
        return curPositionCount;
    }

    protected int addBinaryInput(Column[] column, IWindow curWindow) {
        int curPositionCount = column[0].getPositionCount();

        for (int i = 0; i < curPositionCount; i++) {
            // skip null value in control column
            if (column[0].isNull(i)) {
                continue;
            }
            if (!curWindow.satisfy(column[0], i)) {
                return i;
            }
            curWindow.mergeOnePoint();
            if (!column[2].isNull(i)) {
                updateBinaryFirstValue(column[2].getBinary(i), column[1].getLong(i));
            }
        }
        return curPositionCount;
    }
}
